<DataGrid x:Class="ZOIL.Library.Components.VHyperGrid2c" x:Name="HyperGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:s="http://schemas.microsoft.com/surface/2008" xmlns:p="http://hci.uni-konstanz.de/permaedia" Template="{DynamicResource HyperGridTemplate}">
    <DataGrid.Resources>
        <!-- Default Colors -->
        <SolidColorBrush x:Key="DefaultControlForeground" Color="#FF000000" />
        <SolidColorBrush x:Key="GroupHeaderBackground" Color="#AA333333" />
        <SolidColorBrush x:Key="HeaderMouseOverBackgroundBrush" Color="#CC444444" />
        <SolidColorBrush x:Key="HeaderSortedBorderBrush" Color="#FF444444" />
        <SolidColorBrush x:Key="HeaderSortedBackgroundBrush" Color="#CC333333" />
        <SolidColorBrush x:Key="HeaderBackgroundBrush" Color="#AA333333" />
        <SolidColorBrush x:Key="HeaderBorderBrush" Color="#AA444444" />
        <SolidColorBrush x:Key="HeaderHighlightedBackgoundBrush" Color="#CC333333" />
        <SolidColorBrush x:Key="HeaderPressedBackgroundBrush" Color="#AA444444" />

        <!-- ColumnHeaderStyle -->
        <!-- see http://blogs.msdn.com/jaimer/archive/2009/01/20/styling-microsoft-s-wpf-datagrid.aspx -->
        <Style x:Key="ColumnHeaderGripperStyle" TargetType="{x:Type Thumb}">
            <Setter Property="Width" Value="5" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="Cursor" Value="SizeWE" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Thumb}">
                        <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        
        <Style x:Key="HGColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}">
            <Setter Property="SnapsToDevicePixels" Value="True" />
            <Setter Property="MinWidth" Value="0" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridColumnHeader}" x:Name="ColumnHeaderTemplate">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="16" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="22" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Border x:Name="BackgroundBorder" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" BorderThickness="0,1,0,1" Background="{StaticResource HeaderSortedBackgroundBrush}" BorderBrush="{StaticResource HeaderSortedBorderBrush}" Grid.ColumnSpan="2" />
                            <!-- Header Title -->
                            <ContentPresenter Grid.Row="0" Grid.Column="0" Margin="3,0,3,0" VerticalAlignment="Center" />
                            <!-- Filter Box -->
                            <s:SurfaceTextBox x:Name="Filter_TextBox" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="1,1,1,1" Height="26" HorizontalAlignment="Stretch" VerticalContentAlignment="Center" TextChanged="Filter_TextBox_TextChanged" Loaded="Filter_TextBox_Loaded" />
                            <TextBlock x:Name="Filter_Overlay" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="3,10,3,1" Height="26" HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="Enter Filter here" Foreground="Gray" Visibility="{Binding ElementName=Filter_TextBox, Path=Text.Length, Converter={p:VisibilityLengthConverterInverse}}" IsHitTestVisible="False" />
                            <!-- Reset Filter Button -->
                            <Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,2,0" Visibility="{Binding ElementName=Filter_TextBox, Path=Text.Length, Converter={p:VisibilityLengthConverter}}">
                                <Button x:Name="Filter_ResetButton" Background="Transparent" Foreground="Transparent" Width="24" Height="24" MinHeight="24" MinWidth="24" Visibility="Visible" Click="Filter_Textbox_Reset" />
                                <Image Source="http://hci.uni-konstanz.de/MedioVis/HyperGrid_Images/Undo-16x16.png" HorizontalAlignment="Center" VerticalAlignment="Center" Width="16" Height="16" IsHitTestVisible="False" />
                            </Grid>

                            <Path x:Name="SortArrow" Grid.Row="0" Grid.Column="1" Visibility="Collapsed" Data="M0,0 L1,0 0.5,1 z" Stretch="Fill" Width="8" Height="6" Fill="White" Margin="0,0,8,0" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.4" />
                            <Rectangle Width="1" Fill="#AAC377" HorizontalAlignment="Right" Grid.ColumnSpan="2" Grid.RowSpan="2" />
                            <Rectangle Width="1" Fill="#425B10" HorizontalAlignment="Right" Grid.ColumnSpan="2" Grid.RowSpan="2" Margin="0,0,1,0" />
                            <Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" VerticalAlignment="Stretch" Grid.ColumnSpan="2" Grid.RowSpan="2" Style="{StaticResource ColumnHeaderGripperStyle}" />
                            <Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" VerticalAlignment="Stretch" Grid.ColumnSpan="2" Grid.RowSpan="2" Style="{StaticResource ColumnHeaderGripperStyle}" />

                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="SortDirection" Value="{x:Null}">
                                <Setter TargetName="BackgroundBorder" Property="Background" Value="{DynamicResource HeaderBackgroundBrush}" />
                                <Setter TargetName="BackgroundBorder" Property="BorderBrush" Value="Transparent" />
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True" />
                                    <Condition Property="SortDirection" Value="{x:Null}" />
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="BackgroundBorder" Value="{StaticResource HeaderMouseOverBackgroundBrush}" />
                                <Setter Property="BorderBrush" TargetName="BackgroundBorder" Value="{StaticResource HeaderBorderBrush}" />
                            </MultiTrigger>

                            <Trigger Property="SortDirection" Value="Ascending">
                                <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                                <Setter TargetName="SortArrow" Property="RenderTransform">
                                    <Setter.Value>
                                        <RotateTransform Angle="180" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                            <Trigger Property="SortDirection" Value="Descending">
                                <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
                            </Trigger>
                            <!--<Trigger Property="DisplayIndex" Value="0">
                                <Setter Property="Visibility" Value="Collapsed" 
                                  TargetName="PART_LeftHeaderGripper"></Setter>
                            </Trigger>-->
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!--row header template-->
        <DataTemplate x:Key="RowHeaderTemplate">
            <s:SurfaceCheckBox IsChecked="{Binding Path=IsSelected, &#xD;&#xA;                               Mode=TwoWay, &#xD;&#xA;                               RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}" VerticalAlignment="Top" MinHeight="15" MinWidth="15" Height="15" Width="15" />
        </DataTemplate>

        <Style TargetType="{x:Type DataGridCellsPresenter}">
            <EventSetter Event="PreviewMouseLeftButtonDown" Handler="rowZoomInto" />
            <EventSetter Event="PreviewMouseRightButtonDown" Handler="rowZoomOut" />
            <!--<EventSetter Event="UIElement.ContactTapGesture" Handler="surface_rowZoom" />-->
            <!--<EventSetter Event="s:Contacts.PreviewContactDown" Handler="surface_rowZoom" />-->
            <Style.Triggers>
                <!-- TODO more sophisticated mouse-over styles - see:
                            http://www.silverlightshow.net/items/Changing-the-style-of-the-selected-row-of-the-DataGrid-control.aspx
                            http://stackoverflow.com/questions/382249/how-to-remove-the-mouseover-highlight-and-selected-row-highlight-in-a-silverlight
                            WORKING TIP:
                            http://www.codeplex.com/wpf/Thread/View.aspx?ThreadId=36225
                    -->
                <Trigger Property="DataGridCellsPresenter.IsMouseOver" Value="True">
                    <Setter Property="DataGridCellsPresenter.Background" Value="Gray" />
                    <Setter Property="DataGridCellsPresenter.Foreground" Value="Black" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <!-- SelectAllButton ControlTemplate -->
        <ControlTemplate x:Key="SelectAllButtonTemplate" TargetType="{x:Type Button}">
            <Grid>
                <s:SurfaceCheckBox x:Name="checkbox" HorizontalAlignment="Center" VerticalAlignment="Bottom" Width="15" Height="15" />
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="checkbox" Property="IsChecked" Value="True" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <!-- complete HyperGrid template (scrollviewer, header, ...)-->
        <ControlTemplate x:Key="HyperGridTemplate" TargetType="{x:Type DataGrid}">
            <Border SnapsToDevicePixels="True" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
                <s:SurfaceScrollViewer x:Name="DG_ScrollViewer" Focusable="False">
                    <s:SurfaceScrollViewer.Template>
                        <ControlTemplate TargetType="{x:Type s:SurfaceScrollViewer}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>
                                <!--select all button-->
                                <Button Focusable="False" Width="{Binding Path=CellsPanelHorizontalOffset,&#xD;&#xA;                                                        RelativeSource={RelativeSource FindAncestor, &#xD;&#xA;                                                                                       AncestorLevel=1,&#xD;&#xA;                                                                                       AncestorType={x:Type DataGrid}}}" Template="{StaticResource SelectAllButtonTemplate}">
                                    <Button.Visibility>
                                        <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor,&#xD;&#xA;                                                                                 AncestorLevel=1,&#xD;&#xA;                                                                                 AncestorType={x:Type DataGrid}}">
                                            <Binding.ConverterParameter>
                                                <DataGridHeadersVisibility>All</DataGridHeadersVisibility>
                                            </Binding.ConverterParameter>
                                        </Binding>
                                    </Button.Visibility>
                                    <Button.Command>
                                        <RoutedCommand />
                                    </Button.Command>
                                </Button>
                                <!--column headers-->
                                <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1">
                                    <DataGridColumnHeadersPresenter.Visibility>
                                        <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}">
                                            <Binding.ConverterParameter>
                                                <DataGridHeadersVisibility>Column</DataGridHeadersVisibility>
                                            </Binding.ConverterParameter>
                                        </Binding>
                                    </DataGridColumnHeadersPresenter.Visibility>
                                </DataGridColumnHeadersPresenter>
                                <!--area above scrollbar-->
                                <Grid Grid.Column="2">
                                    <!--<s:SurfaceSlider Orientation="Vertical"
                                                     IsDirectionReversed="True"
                                                     Value="{Binding ElementName=HyperGrid, Path=RowHeight}"
                                                     Minimum="{Binding ElementName=HyperGrid, Path=MinRowHeight}"
                                                     Maximum="{Binding ElementName=HyperGrid, Path=RowHeightZoomed}"
                                                     Loaded="RowHeightZoomSlider_Loaded"
                                                     ValueChanged="RowHeightZoomSlider_ValueChanged"/>-->
                                </Grid>
                                <!--scrollcontent-->
                                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" ContentTemplate="{TemplateBinding ContentTemplate}" Grid.ColumnSpan="2" Grid.Row="1" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" />
                                <s:SurfaceScrollBar Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" x:Name="PART_VerticalScrollBar" Grid.Column="2" Grid.Row="1" Maximum="{TemplateBinding ScrollableHeight}" Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Vertical" ViewportSize="{TemplateBinding ViewportHeight}" />
                                <Grid Grid.Column="1" Grid.Row="2">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="{Binding Path=NonFrozenColumnsViewportHorizontalOffset, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type DataGrid}}}" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <s:SurfaceScrollBar Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="{TemplateBinding ScrollableWidth}" Value="{Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" />
                                </Grid>
                            </Grid>
                        </ControlTemplate>
                    </s:SurfaceScrollViewer.Template>
                    <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </s:SurfaceScrollViewer>
            </Border>
        </ControlTemplate>
    </DataGrid.Resources>
</DataGrid>
